from django.db import models

YESANDNO = (
    (0, "否"),
    (1, "是"),
)


# 管理员
class Staff(models.Model):
    openid = models.CharField("openid", max_length=50, null=True, blank=True)
    dept = models.CharField("部门", max_length=50)
    name = models.CharField("姓名", max_length=50)
    phone = models.CharField("手机", max_length=50, unique=True)
    is_leader = models.IntegerField("是否超级管理员", choices=YESANDNO, default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    @property
    def is_authenticated(self):
        return True

    def __str__(self):
        return f"管理员<{self.name}>"

    class Meta:
        unique_together = ("openid", "phone")
        verbose_name = verbose_name_plural = "管理员"


class Plate(models.Model):
    KINDS = (
        (0, "电动自行车"),
        (1, "自行车"),
    )
    kind = models.IntegerField("种类", choices=KINDS, default=0)
    number = models.CharField("号码", max_length=16, unique=True)
    STATUS_CHOISE = (
        (0, "未绑定"),
        (1, "已绑定"),
        (2, "黑名单"),
    )
    status = models.IntegerField("使用状态", choices=STATUS_CHOISE, default=0)
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"牌照-{self.number}"

    class Meta:
        verbose_name = verbose_name_plural = "牌照"


class Car(models.Model):
    plate = models.OneToOneField(Plate, on_delete=models.CASCADE, verbose_name="牌照")
    # 品牌型号
    brand = models.CharField("品牌型号", max_length=50)
    color = models.CharField("颜色", max_length=50)
    is_ccc = models.IntegerField("是否CCC认证", choices=YESANDNO, default=1)
    photos = models.CharField("照片", max_length=2048)
    KINDS = (
        (0, "学生车辆"),
        (1, "教师车辆"),
    )
    kind = models.IntegerField("种类", choices=KINDS, default=0)

    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"车辆<{self.plate.__str__()}>"

    class Meta:
        verbose_name = verbose_name_plural = "车辆管理"


class Student(models.Model):
    car = models.OneToOneField(Car, on_delete=models.CASCADE, verbose_name="车辆")
    college = models.CharField("学院", max_length=50)
    major = models.CharField("专业", max_length=50)
    grade = models.CharField("年级", max_length=50)
    stuid = models.CharField("学号", max_length=50, unique=True)
    name = models.CharField("姓名", max_length=50)
    phone = models.CharField(" ", max_length=50, unique=True)

    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"学生<{self.name}>"

    class Meta:
        verbose_name = verbose_name_plural = "学生管理"


class Teacher(models.Model):
    car = models.OneToOneField(Car, on_delete=models.CASCADE, verbose_name="车辆")
    dept = models.CharField("部门", max_length=50)
    name = models.CharField("姓名", max_length=50)
    phone = models.CharField("手机", max_length=50, unique=True)

    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"教师<{self.name}>"

    class Meta:
        verbose_name = verbose_name_plural = "教师管理"


# violation
class Violation(models.Model):
    car = models.ForeignKey(Car, on_delete=models.CASCADE, verbose_name="车辆")
    reason = models.CharField("原因", max_length=256)
    # 照片
    photos = models.CharField("照片", max_length=2048)
    # 记录人
    staff = models.ForeignKey(Staff, on_delete=models.CASCADE, verbose_name="记录人")
    created = models.DateTimeField("创建时间", auto_now_add=True, null=True, blank=True)
    updated = models.DateTimeField("更新时间", auto_now=True, null=True, blank=True)

    def __str__(self):
        return f"{self.car.plate.number}违规"

    class Meta:
        verbose_name = verbose_name_plural = "违规记录"
        ordering = ["-created"]
